package ui.fragment.group_management;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import adapter.group_management.GroupManagementAdapter;
import controller.company.SelectCompanyAsync;
import controller.group_management.GroupManagementAsync;
import ui.activity.login.Login;

public class GroupManagementFragment extends Fragment
        implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener{

    /**
     * String section
     */

    /**
     * View section
     */

    public static GridView mGvInGroupManagementFragment;
    public static GroupManagementAdapter groupManagementAdapter;
    private SwipeRefreshLayout mSrlInGroupManagementFragment;
    /**
     * Others section
     */
    public  static Context mContext;
    /**
     * Listener section
     */

    public static Fragment newInstance() {

        GroupManagementFragment fragment = new GroupManagementFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_group_management, container, false);
        // Initial views
        mContext = getActivity();
        initialViews(v);
        initialData();
        setDataForGroupManagementFragment(mContext);
        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //TODO will remove after finish all ibtn on actionbar
        //TODO mean write function invisible all the others ibtn and only visible ibtn at current page
        //invisible ibtn create group
        Enterprise.mIbtnCreateGroup.setVisibility(View.GONE);
        Enterprise.mIbtnUpload.setVisibility(View.VISIBLE);
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);

    }

    @Override
    public void onRefresh() {
        // what's thing need refresh when pull down, need place in here
        try {
            //Call new thread to get Data
            Log.i("", "refresh data");
            if (!GroupManagementAsync.mAlGroupId.isEmpty())
                GroupManagementAsync.mAlGroupId.clear();
            if (!GroupManagementAsync.mAlGroupManagement.isEmpty())
                GroupManagementAsync.mAlGroupManagement.clear();
            groupManagementAdapter.notifyDataSetChanged();
            setDataForGroupManagementFragment(mContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // what's thing need refresh when pull down, need place in here
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    // Stop the animation once we are done fetching data.
                    mSrlInGroupManagementFragment.setRefreshing(false);
                    /**
                     * You can add code to update your list with the new data.
                     **/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 4000);
    }

    /**
     * Initialize methods
     */
    private void initialData() {
        groupManagementAdapter = new GroupManagementAdapter(getActivity(),
                R.layout.simple_grid_item_group_management, GroupManagementAsync.mAlGroupManagement);
        mGvInGroupManagementFragment.setAdapter(groupManagementAdapter);
        mSrlInGroupManagementFragment.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        mSrlInGroupManagementFragment.setOnRefreshListener(this);
    }

    private void initialViews(View v) {
        mGvInGroupManagementFragment = (GridView) v.findViewById(R.id.gv_in_group_management_fragment);
        mSrlInGroupManagementFragment = (SwipeRefreshLayout) v.findViewById(R.id.srl_in_group_management_fragment);
        //visible ibtn create group
        Enterprise.mIbtnCreateGroup.setVisibility(View.VISIBLE);
        Enterprise.mIbtnUpload.setVisibility(View.GONE);
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);
    }

    /**
     * The others methods
     */
    public static void setDataForGroupManagementFragment(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new GroupManagementAsync(context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    Login.user.getUserInfo().getAccessToken(),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyIdGroup()));
        else
            new GroupManagementAsync(context).execute(
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    Login.user.getUserInfo().getAccessToken(),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyIdGroup()));
    }
}
